package hu;


public class Test4 extends Thread {
    static int n = 5; // 线程数量
    static int cnt = 0; // 计数，到100结束
    private int id;  // 线程编号
    public Test4(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (Test4.class) {
                Test4.class.notifyAll();
                if(cnt >= 100){
                    break;
                }
                if (cnt % n == id){
                    System.out.println("thread_" + id + " cnt:" + ++cnt);
                }try {
                    Test4.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        for(int i = 0; i < n; i++){
            new Test4(i).start();
        }
    }
}
